package com.ternnetwork.core.configuration.service.impl;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.accesscontroll.entity.User;
import com.ternnetwork.core.accesscontroll.service.UserService;
import com.ternnetwork.core.configuration.dto.DataDictionaryAddDto;
import com.ternnetwork.core.configuration.dto.DataDictionaryEditDto;
import com.ternnetwork.core.configuration.entity.DataDictionary;
import com.ternnetwork.core.configuration.enums.DataDictionaryMessages;
import com.ternnetwork.core.configuration.repository.DataDictionaryRepository;
import com.ternnetwork.core.configuration.service.DataDictionaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {

    @Autowired
    private DataDictionaryRepository dataDictionaryRepository;
    @Autowired
    private UserService userService;

    @CacheEvict(value="dataDictionary",key="#dto.key")
    @Override
    public BaseResponse add(DataDictionaryAddDto dto, String username) {

        BaseResponse  baseResponse=checkProperties(dto);

        if(baseResponse.getStatus().intValue()==0) {

            return baseResponse;
        }

        User loginUser=userService.findByName(username);

        long totalCount=dataDictionaryRepository.getCountByKey(dto.getKey());

        if(totalCount>0L) {
            baseResponse.setFailed(DataDictionaryMessages.FAILED_MESSAGE_KEY_CAN_NOT_BE_DUPLICATE.getName());
            return baseResponse;
        }

        DataDictionary dataDictionary=new DataDictionary();

        BeanUtils.copyProperties(dto,dataDictionary);

        dataDictionary.setCreatedById(loginUser.getId());

        dataDictionary.setCreateTime(new Date());

        dataDictionaryRepository.saveAndFlush(dataDictionary);

        baseResponse.setSccucess();

        return baseResponse;
    }
    @CacheEvict(value="dataDictionary",key="#dto.key")
    @Override
    public BaseResponse update(DataDictionaryEditDto dto, String username) {

        BaseResponse  baseResponse=checkProperties(dto);

        if(baseResponse.getStatus().intValue()==0) {

            return baseResponse;
        }

        Optional<DataDictionary> optional=dataDictionaryRepository.findById(dto.getId());

        if(optional.equals(Optional.empty())){

            baseResponse.setFailedForObjectNotExist();

            return baseResponse;
        }
        DataDictionary dataDictionary =optional.get();

        long totalCount=dataDictionaryRepository.getCountByKeyAndExcludeId(dto.getKey(),dto.getId());

        if(totalCount>0L) {
            baseResponse.setFailed(DataDictionaryMessages.FAILED_MESSAGE_KEY_CAN_NOT_BE_DUPLICATE.getName());
            return baseResponse;
        }

        BeanUtils.copyProperties(dto,dataDictionary);

        dataDictionaryRepository.saveAndFlush(dataDictionary);

        baseResponse.setSccucess();

        return baseResponse;
    }

    @CacheEvict(value="dataDictionary",key="#key")
    @Override
    public BaseResponse deleteById(Long id,String key) {

        BaseResponse  baseResponse=new BaseResponse();

        Optional<DataDictionary> optional=dataDictionaryRepository.findById(id);

        if(optional.equals(Optional.empty())){

            baseResponse.setFailedForObjectNotExist();

            return baseResponse;
        }

        long totalCount=dataDictionaryRepository.getCountByParentId(id);

        if(totalCount>0L) {
            baseResponse.setFailed(DataDictionaryMessages.FAILED_MESSAGE_CAN_NOT_DELETE_FOR_IT_HAS_CHILDREN.getName());
            return baseResponse;
        }

        DataDictionary dataDictionary=optional.get();

        dataDictionaryRepository.delete(dataDictionary);

        baseResponse.setSccucess();

        return baseResponse;

    }

    @Override
    public BaseResponse findAll() {

        BaseResponse baseResponse = new BaseResponse();

        List<DataDictionary> noteCategoryList=dataDictionaryRepository.findAll();

        baseResponse.setSccucess(noteCategoryList);

        return baseResponse;
    }

    private BaseResponse checkProperties (DataDictionaryAddDto t) {

        BaseResponse  baseResponse=new BaseResponse();

        if(StringUtils.isEmpty(t.getKey())) {

            baseResponse.setFailed(DataDictionaryMessages.FAILED_MESSAGE_KEY_CAN_NOT_BE_EMPTY.getName());

            return baseResponse;
        }

        if(StringUtils.isEmpty(t.getName())) {

            baseResponse.setFailed(DataDictionaryMessages.FAILED_MESSAGE_NAME_CAN_NOT_BE_EMPTY.getName());

            return baseResponse;
        }

        baseResponse.setSccucess();

        return baseResponse;
    }

    @Cacheable(value="dataDictionary",key="#key")
    public String getValueByKey(String key){

        List<DataDictionary> dataDictionaryList=dataDictionaryRepository.findAllByKey(key);

        if(dataDictionaryList.size()>0){
            return dataDictionaryList.get(0).getValue();
        }
        return null;
    }
}
